<template>
    <view class="container">
        <!-- 顶部导航栏 -->
<!--        <view class="nav-bar">
            <view class="title">健康课程</view>
        </view> -->
        
        <!-- 课程分类 -->
        <view class="category-tabs">
            <scroll-view scroll-x="true" class="tabs-scroll">
                <view 
                    v-for="(item, index) in categories" 
                    :key="index"
                    class="tab-item"
                    :class="{ active: currentCategory === item.id }"
                    @click="switchCategory(item.id)"
                >
                    {{item.name}}
                </view>
            </scroll-view>
        </view>
        
        <!-- 课程列表 -->
        <view class="video-list">
            <view 
                class="video-item"
                v-for="video in filteredVideoList"
                :key="video.id"
                @click="goDetail(video.id)"
            >
                <image class="cover" :src="video.videoCover" mode="aspectFill"></image>
                <view class="info">
                    <view class="name">{{video.name}}</view>
                    <!-- <view class="desc">{{video.introduction}}</view> -->
                    <view class="meta">
<!--                        <view class="tutor">
                            <u-icon name="account" size="28" color="#666"></u-icon>
                            <text>{{video.tutor}}</text>
                        </view> -->
                        <view class="tutor">
                            <u-icon name="clock" size="28" color="#666"></u-icon>
                            <text>{{formatDuration(video.duration)}}</text>
                        </view>
                    </view>
                </view>
            </view>
        </view>
        
        <!-- 自定义底部导航栏 -->
        <custom-tabbar></custom-tabbar>
    </view>
</template>

<script>
import { getGuidanceVideoList } from '../../common/api'
import CustomTabbar from '../../components/custom-tabbar/custom-tabbar.vue'

export default {
    components: {
        CustomTabbar
    },
    data() {
        return {
            currentCategory: 0,
			guidanceVideoList: [],
			showCustomTabBar: true,
            categories: [
                { id: 0, name: '全部' },
                { id: 1, name: '认识卒中' },
                { id: 2, name: '卒中识别' },
                { id: 3, name: '危险因素' }
            ],
        }
    },
	
    onLoad() {
  	    this.loadGuidanceVideoList();
		  uni.$on('page-show', () => {
		    if (!this.shouldUpdate) return;
		    this.updateTabBar();
		  });
        // 隐藏原生tabBar
        uni.hideTabBar()
    },
    
    onShow() {
        // 页面显示时隐藏原生tabBar，显示自定义tabBar
        uni.hideTabBar()
        // 通知自定义tabBar更新状态
       // uni.$emit('update-tabbar')
    },
    
    onHide() {
        // 页面隐藏时显示原生tabBar
        uni.showTabBar()
    },
    
    computed: {
        filteredVideoList() {
            if (this.currentCategory === 0) {
                return this.guidanceVideoList;
            }
            return this.guidanceVideoList.filter(video => Number(video.strokeGuidanceType) === this.currentCategory);
        }
    },
  
    methods: {
		async loadGuidanceVideoList() {
			try{
				const qlRes = await getGuidanceVideoList()
				this.guidanceVideoList = qlRes.rows
				console.log('视频：', qlRes.rows)
				// console.log('视频list：', guidanceVideoList)
			}
			catch (e) {
			  console.error('加载指导视频列表失败', e)
			}
		},
        switchCategory(id) {
            this.currentCategory = id;
            // TODO: 根据分类加载课程列表
        },
        goDetail(id) {
            uni.navigateTo({
                url: `/pages/health/course-detail?id=${id}`
            });
        },
		formatDuration(seconds) {
		        if (!seconds && seconds !== 0) return '';
		        const min = Math.floor(seconds / 60);
		        const sec = seconds % 60;
		        return `${min}分${sec}秒`;
		    },
        getVideoCover(video) {
            // 优先使用视频的cover字段
/*            if (video.cover) {
                return this.processVideoUrl(video.cover);
            } */
            // 如果没有cover字段，使用视频URL作为封面
/*            if (video.video) {
                return this.processVideoUrl(video.video);
            } */
            // 默认封面
            // return '/static/images/course-1.jpg';
			
        },
        processVideoUrl(url) {
            if (!url) return '';
            // 检查是否以"http:"开头但缺少第二个斜杠
            if (url.startsWith('http:/') && !url.startsWith('http://')) {
                return url.replace('http:/', 'http://');
            }
            return url;
        },
    }
}
</script>

<style lang="scss" scoped>
.container {
    padding: 0;
    background: #f5f5f5;
    padding-bottom: 120rpx; /* 为底部导航栏留出空间 */
}

.nav-bar {
    background: #fff;
    height: 88rpx;
    display: flex;
    align-items: center;
    padding: 0 30rpx;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 100;
    
    .title {
        font-size: 32rpx;
        font-weight: bold;
        color: #333;
    }
}

.category-tabs {
    // margin-top: 88rpx;
    background: #fff;
    padding: 20rpx 0;
    
    .tabs-scroll {
        white-space: nowrap;
        padding: 0 20rpx;
    }
    
    .tab-item {
        display: inline-block;
        padding: 10rpx 30rpx;
        margin: 0 10rpx;
        font-size: 30rpx;
        color: #666;
        border-radius: 30rpx;
        
        &.active {
            background: #1890FF;
            color: #fff;
        }
    }
}

.video-list {
    padding: 20rpx;
    
    .video-item {
        background: #fff;
        border-radius: 12rpx;
        margin-bottom: 20rpx;
        overflow: hidden;
        
        .cover {
            width: 100%;
            height: 320rpx;
        }
        
        .info {
            padding: 20rpx;
            
            .name {
                font-size: 32rpx;
                font-weight: bold;
                color: #333;
                margin-bottom: 10rpx;
            }
            
            .desc {
                font-size: 26rpx;
                color: #666;
                margin-bottom: 20rpx;
            }
            
            .meta {
                display: flex;
                align-items: center;
                justify-content: space-between;
                
                .tutor, .duration {
                    display: flex;
                    align-items: center;
                    font-size: 24rpx;
                    color: #666;
                    
                    text {
                        margin-left: 8rpx;
                    }
                }
            }
        }
    }
}
</style> 